package com.简单项目.自己写的房屋出租;

import java.util.Scanner;

public class P {
    private boolean bool = true;
    Scanner scanner = new Scanner(System.in);
    private String choice;
    private String list[][] = new String[0][6];
    int x = -1;
    private int choiceint;


    public void methods(){
        do{
           menu();
           choice = scanner.next();
            switch (choice) {
                case "1" -> Add();
                case "2" -> Find();
                case "3" -> Delete();
                case "4" -> Update();
                case "5" -> House_list();
                case "6" -> exit();
                default -> System.out.println("请输入正确的选项");
            }


        }while(bool);
        System.out.println("退出了项目");
    }
    private void menu(){
        System.out.println("\n--------------房屋出租系统-------------");
        System.out.println("              1 新增房源              ");
        System.out.println("              2 查找房源              ");
        System.out.println("              3 删除房源              ");
        System.out.println("              4 修改房源信息           ");
        System.out.println("              5 房屋列表              ");
        System.out.println("              6 退   出              ");
    }

    public void Add(){
        list = ArrayExpansion(list);
        x++;   // 数组扩容
        System.out.println("---------------添加房屋---------------");
                                                   list[x][0] = String.valueOf(x+1);
        System.out.print("姓名： ");                list[x][1] = scanner.next();
        System.out.print("电话： ");                list[x][2] = scanner.next();
        System.out.print("地址： ");                list[x][3] = scanner.next();
        System.out.print("月租： ");                list[x][4] = scanner.next();
        System.out.print("状态（未出租/已出租）： ");  list[x][5] = scanner.next();
        System.out.println("---------------添加完成---------------");

    }
    public void Find(){
        System.out.printf("请输入要查找的房源编号：  ");
        choiceint = scanner.nextInt();//1
        if(choiceint > list.length || choiceint < 1){
            System.out.println("没有该房源");
            return;
        }

        for (int i = 0; i < list.length; i++) {
            if(list[i][0].equals(String.valueOf(choiceint))){
                System.out.println("编号："+ list[i][0]+"\t\t"+"姓名："+ list[i][1]+"\t\t"+"电话："+ list[i][2]+"\t\t"+"地址："+ list[i][3]+"\t\t"+"月租："+ list[i][4]+"\t\t"+"状态："+ list[i][5]);
            }

        }
    }
    public void Delete(){
        System.out.printf("请输入要删除的房源编号：  ");
        choiceint = scanner.nextInt();//1
        if(choiceint > list.length || choiceint < 1){
            System.out.println("没有该房源");
            return;
        }
        if(choiceint == list.length ){
            list = ArrayCurtail(list);
            x--;// 数组缩容
        }else{
            for (int i = choiceint; i < list.length; i++) {
                for (int j = 0; j < list[i].length; j++) {
                    list[i-1][j] = list[i][j];
                }
            }
            list = ArrayCurtail(list); //先从整体  choiceint-1处  往前移 再数组缩容
            x--;
        }
        System.out.println("------------删除成功------------");


    }
    public void Update(){
        System.out.printf("请输入要修改的房源编号：  ");
        choiceint = scanner.nextInt();//1
        if(choiceint > list.length || choiceint < 1){
            System.out.println("没有该房源");
            return;
        }
        System.out.print("姓名："+"("+list[choiceint-1][1]+")\t");                list[choiceint-1][1] = scanner.next();
        System.out.print("电话："+"("+list[choiceint-1][2]+")\t");                list[choiceint-1][2] = scanner.next();
        System.out.print("地址："+"("+list[choiceint-1][3]+")\t");                list[choiceint-1][3] = scanner.next();
        System.out.print("月租："+"("+list[choiceint-1][4]+")\t");                list[choiceint-1][4] = scanner.next();
        System.out.print("状态："+"("+list[choiceint-1][5]+")\t");                list[choiceint-1][5] = scanner.next();
        System.out.println("---------------修改完成---------------");


    }

    public void House_list() {

        System.out.println("------------房屋列表------------");
        if(list.length<1){
            System.out.println("暂无房屋信息");
            return;
        }
        System.out.println("编号\t\t姓名\t\t电话\t\t地址\t\t月租\t\t状态");

        for (int i = 0; i < list.length; i++) {
            for (int j = 0; j < list[i].length; j++) {
                if(list[i][j] != null)
                    System.out.print(list[i][j] + "\t\t");
            }
            System.out.println();
        }
    }
    public void exit() {
        System.out.println("请输入你的选择（y/n）");
        do{
            choice = scanner.next();
            if(choice.equals("y")){
                bool = false;
                return;
            }
            else if(choice.equals("n")){
                return;
            }
            else{
                System.out.println("请输入正确的选项（y/n）");
            }
            }while(true);

    }

    public String[][] ArrayExpansion(String[][] list){
        String newList[][] = new String[list.length+1][6];
        for (int i = 0; i < list.length; i++) {
            for (int j = 0; j < list[i].length; j++) {
                if(list.length>0)
                    newList[i][j] = list[i][j];
            }
        }
        for (int i = 0; i < 6; i++) {
            newList[list.length][i] = "0";

        }
        return newList;
    }

    public String[][] ArrayCurtail(String[][] list){
        String newList[][] = new String[list.length-1][6];

        for (int i = 0; i < list.length-1; i++) {
            for (int j = 0; j < list[i].length; j++) {
                if(list.length>0)
                    newList[i][j] = list[i][j];
            }
        }
        return newList;
    }



}
